package com.chen.base.interceptor;

import cn.hutool.core.util.StrUtil;
import com.chen.base.utils.SpringContextHolder;
import com.chen.root.constant.SecurityCons;
import com.chen.root.property.GatewaySecurityProperties;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: ~龙~
 * @DATE: 2023/11/24
 **/
public class GatewaySecurityInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        GatewaySecurityProperties bean = SpringContextHolder.getBean(GatewaySecurityProperties.class);
        String securityCode = request.getHeader(SecurityCons.GATEWAY_SECURITY_CODE);
        if (StrUtil.isNotBlank(securityCode) && securityCode.equals(bean.getSecurityCode())){
            return true;
        }
        throw new RuntimeException("非法请求!");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

    }
}
